package com.woniu.jifen.controller;

import com.alibaba.fastjson2.JSON;
import com.woniu.common.dto.UserDto;
import com.woniu.common.util.GetUserInfoUtil;
import com.woniu.common.util.ResponseData;
import com.woniu.jifen.entity.TGoods;
import com.woniu.jifen.entity.TProductSku;
import com.woniu.jifen.entity.TProductSpu;
import com.woniu.jifen.service.TGoodsService;
import com.woniu.jifen.service.TProductSkuService;
import com.woniu.jifen.service.TProductSpuService;
import com.woniu.common.util.ResponseEnum;
import com.woniu.jifen.entity.*;
import com.woniu.jifen.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author ：sw
 * @Date 2024年05月21日   19:29
 * @Decription
 */
@RestController
@Api(tags = "积分商城API")
public class JiFenController {
    @Autowired
    private TProductSpuService spuService;

    @Autowired
    private TProductSkuService skuService;

    @Autowired
    private TGoodsService goodsService;
    /**
     * 查商品
     */
    @PostMapping("/querySpus")
    @ApiOperation(value = "获取商品列表",notes = "获取商品列表")
    public ResponseData<TProductSpu> querySpus() {
        List<TProductSpu> skuList = spuService.lambdaQuery()
                .eq(TProductSpu::getState, 2).list();
        return new ResponseData<>().ok(skuList);
    }

    @PostMapping("/querySkus")
    @ApiOperation(value = "获取商品列表",notes = "获取商品列表")
    public ResponseData<TProductSku> querySkus() {
        List<TProductSku> skuList = skuService.lambdaQuery()
                .eq(TProductSku::getState, 2).list();
        return new ResponseData<>().ok(skuList);
    }

    @GetMapping("/querySkuById")
    public ResponseData<TProductSku> querySkuById(Long id){
        TProductSku sku = skuService.lambdaQuery().eq(TProductSku::getId, id).one();
        return new ResponseData<>().ok(sku);
    }


    /**
     * 存抽中物品
     */
    @PostMapping("/addGoods")
    public ResponseData<?> addGoods(@RequestBody TGoods goods){
        return goodsService.addGoods(goods);

    }

    /**
     * 查所有的抽中记录
     */
    @GetMapping("/queryGoods")
    public List<TGoods> queryGoods() {
        return goodsService.queryGoods();
    }

    @Autowired
    private TUserService userService;
    @Autowired
    private TOrderService orderService;
    @Autowired
    private TOrderDetailService orderDetailService;
    /**
     * 话费支付积分
     */
    @SneakyThrows
    @GetMapping("/payJifen/{orderNum}")
    @ApiOperation(value = "话费支付积分",notes = "话费支付积分")
    @Transactional(rollbackFor = Exception.class)
    public ResponseData<TUser> payJifen(@PathVariable("orderNum") Long orderNum) {
        UserDto userInfo = GetUserInfoUtil.getUserInfo();
        TOrder one = orderService.lambdaQuery().eq(TOrder::getId, orderNum).one();
        if (one==null && one.getIntegral()==null  ){
            return new ResponseData<>().fail(ResponseEnum.FAIL);
        }
        if (one.getStatus()==4 && one.getStatus()==5){
            return new ResponseData<>().fail(ResponseEnum.PAY_AGAIN);
        }
        userService.lambdaUpdate().setSql("integral=integral-"+one.getIntegral())
                .eq(TUser::getPhone,userInfo.getTelephone())
                .update();
        orderService.lambdaUpdate().eq(TOrder::getId, orderNum).set(TOrder::getStatus, 4).update();
        orderDetailService.lambdaUpdate().eq(TOrderDetail::getOrderNum, orderNum).set(TOrderDetail::getStatus, 4).update();
        return new ResponseData<>().ok();
    }

    /**
     * 商品支付积分
     */
    @SneakyThrows
    @GetMapping("/skuPayJifen/{orderNum}")
    @ApiOperation(value = "商品支付积分",notes = "商品支付积分")
    @Transactional(rollbackFor = Exception.class)
    public ResponseData<TUser> skuPayJifen(@PathVariable("orderNum") Long orderNum) {

        TOrder one = orderService.lambdaQuery().eq(TOrder::getId, orderNum).one();
        TOrderDetail one1 = orderDetailService.lambdaQuery().eq(TOrderDetail::getOrderNum, orderNum).one();
        TProductSku sku = skuService.lambdaQuery().eq(TProductSku::getId, one1.getItemId()).one();
        if (one==null && one.getIntegral()==null && sku.getSkuStore()<one1.getBuyNum()){
            return new ResponseData<>().fail(ResponseEnum.FAIL);
        }
        if (one.getStatus()==108 && one.getStatus()==115 ){
            return new ResponseData<>().fail(ResponseEnum.PAY_AGAIN);
        }
        userService.lambdaUpdate().setSql("integral=integral-"+one.getIntegral())
                .eq(TUser::getPhone,one.getUserTelephone())
                .update();
        orderService.lambdaUpdate().eq(TOrder::getId, orderNum).set(TOrder::getStatus, 108).update();
        orderDetailService.lambdaUpdate().eq(TOrderDetail::getOrderNum, orderNum).set(TOrderDetail::getStatus, 108).update();
        skuService.lambdaUpdate().setSql("sku_store=sku_store-"+one1.getBuyNum()).eq(TProductSku::getId,one1.getItemId()).update();
        return new ResponseData<>().ok();
    }
    @Autowired
    private AActivityService activityService;
    /**
     * 新增秒杀活动
     */
    @PostMapping("/newKill")
    @ApiOperation(value = "新增秒杀活动",notes = "新增秒杀活动")
    public ResponseData<AActivity> newKill(@RequestBody AActivity activity) {
        activity.setId(null);
        activityService.save(activity);

        return new ResponseData<>().ok(JSON.toJSONString(activity));

    }
}
